package com.simpletour.qa.service;


import org.apache.commons.io.FileUtils;
import org.junit.Test;

import java.io.File;
import java.io.IOException;

public class RestDocCombine {

    @Test
    public void adocBuild() throws IOException {
        String appDir = System.getProperty("user.dir");
        String adocPath = appDir + "/src/main/asciidoc/index.adoc";
        StringBuilder content = new StringBuilder();
//        content.append("include::" + appDir + "/src/docs/api/asciidocs/preview.adoc");

        File apidirs = new File(appDir + "/custom");
        int  i = 0;
        for (File apidir : apidirs.listFiles()) {
            i++;
            String titleNum = i+".1";
            String apiName = apidir.getName();
            content.append("=== "+titleNum+" " + apiName + "\n\n");
            fileAppend(content, apidir + "/request-headers.adoc", "=== "+titleNum+".1"+"请求头示例");
            fileAppend(content, apidir + "/http-request.adoc", "=== "+titleNum+".2"+"请求示例");
            fileAppend(content, apidir + "/request-parameters.adoc", "=== "+titleNum+".3"+"请求参数示例");
            fileAppend(content, apidir + "/request-body.adoc", "=== "+titleNum+".4"+"请求体示例");
            fileAppend(content, apidir + "/http-response.adoc", "=== "+titleNum+".5"+"响应示例");
            fileAppend(content, apidir + "/response-fields.adoc", "=== "+titleNum+".6"+"响应体示例");
            content.append("\n");
        }
        File file = new File(adocPath);
        FileUtils.writeStringToFile(file, content.toString(), "utf-8");
    }

    private void fileAppend(StringBuilder content, String s, String s1) {
        content.append(s1).append("\n");
        content.append("include::").append(s).append("[]").append("\n");
    }
}

